const app = getApp();
var Mcaptcha = require('../../../utils/mcaptcha.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    RecType: [],
    FenLeiArr: '请选择回收种类',
    addrtishi: '请选择所在地区',
    FenLei: -1,
    Address: '',
    selectS: false,
    animationData: {},
    Province: 0,
    ProvCity: [],
    proName: '请选择',
    CityArr: [],
    City: 0,
    CityName: '',
    AreaArr: [],
    Area: 0,
    AreaName: '',
    StreetArr: [],
    Street: 0,
    StreetName: '',
    tit_step: 1,
    num: '',
    codename: '获取验证码',
    disabled: true,
    LinkPhone: '',
    iscode: '',
    LinkPhone1: '',
    inputPyzm: 'a',
    inputyzm: ''

  },


  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },

  setpSet: function (e) {
    console.log(e)
    this.setData({
      tit_step: e.currentTarget.dataset.step
    })
  },
  selectSereet: function (e) {
    this.setData({
      Street: e.currentTarget.dataset.id,
      StreetName: e.currentTarget.dataset.name,
    })
    var addrtishi = this.data.proName + '-' + this.data.CityName + '-' + this.data.AreaName + '-' + this.data.StreetName;
    this.setData({
      addrtishi: addrtishi
    })
    this.selectH();
  },
  selectP: function (e) {

    var CityArr = this.data.ProvCity[e.currentTarget.dataset.idx].list;
    CityArr.splice(0, 1);
    if (e.currentTarget.dataset.id != this.data.Province) {
      this.setData({
        AreaArr: [],
        Area: 0,
        AreaName: '',
        StreetArr: [],
        Street: 0,
        StreetName: '',
      })
    }
    this.setData({
      CityName: '请选择',
      Province: e.currentTarget.dataset.id,
      proName: e.currentTarget.dataset.name,
      tit_step: 2,
      CityArr: CityArr
    })
  },
  selectC: function (e) {
    var that = this;
    var api_input = {
      pid: e.currentTarget.dataset.id,
      lev: 3
    }
    app.getDaoPostN('api/FJDispose/FJ_Get_GetRegion', api_input, r => {
      if (r.code == 200) {
        if (that.data.City != e.currentTarget.dataset.id) {
          that.setData({
            StreetArr: [],
            Street: 0,
            StreetName: '',
          })
        }
        that.setData({
          AreaArr: r.data,
          City: e.currentTarget.dataset.id,
          CityName: e.currentTarget.dataset.name,
          tit_step: 3,
          AreaName: '请选择',
        })
      }
    })
  },
  selectA: function (e) {
    var that = this;
    var api_input = {
      pid: e.currentTarget.dataset.id,
      lev: 4
    }
    app.getDaoPostN('api/FJDispose/FJ_Get_GetRegion', api_input, r => {
      if (r.code == 200) {
        that.setData({
          StreetArr: r.data,
          Area: e.currentTarget.dataset.id,
          AreaName: e.currentTarget.dataset.name,
          tit_step: 4,
          StreetName: '请选择',
        })
      }
    })
  },
  selectS: function () {
    this.setData({
      selectS: true
    })
    var that = this;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    })
    this.animation = animation
    setTimeout(function () {
      that.fadeIn();//调用显示动画
    })
  },
  selectH: function () {
    var that = this;
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: 'ease',
    })
    this.animation = animation
    that.fadeDown();//调用隐藏动画   
    setTimeout(function () {
      if (that.data.Street == 0) {
        that.setData({
          selectS: false,
          proName: '请选择',
          Province: 0,
          City: 0,
          CityName: '',
          Area: 0,
          AreaName: '',
          Street: 0,
          StreetName: '',
          tit_step: 1,
          addrtishi: '请选择所在地区',
        })
      } else {
        that.setData({
          selectS: false,
        })
      }

    }, 200)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFJ_Get_RecType();
    this.getFJ_Get_ProvCity();
  },
  bindFenLeiChange: function (e) {

    this.setData({
      FenLeiArr: this.data.RecType[e.detail.value].RT_classname,
      FenLei: this.data.RecType[e.detail.value].id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.changePyzm();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  getFJ_Get_RecType: function () {
    var that = this;
    var timestamp = Date.parse(new Date());
    var storageRecType = wx.getStorageSync('storageRecType');
    var storageRecType_tamp = wx.getStorageSync('storageRecType_tamp');
    if (storageRecType && storageRecType_tamp > timestamp) {
      storageRecType.splice(0, 1);
      this.setData({
        RecType: storageRecType
      })
    } else {
      app.getDaoPostN('api/FJDispose/FJ_Get_RecType', '{}', function (r) {

        if (r.code == 200) {
          wx.setStorageSync('storageRecType', r.data);
          wx.setStorageSync('storageRecType_tamp', timestamp + 604800000);
          r.data.splice(0, 1)
          that.setData({
            RecType: r.data
          })
        }
      })
    }
  },
  getFJ_Get_ProvCity: function () {
    var that = this;
    var timestamp = Date.parse(new Date());
    var storageProvCity = wx.getStorageSync('storageProvCity');
    var storageProvCity_tamp = wx.getStorageSync('storageProvCity_tamp');
    if (storageProvCity && storageProvCity_tamp > timestamp) {
      storageProvCity.splice(0, 1);
      this.setData({
        ProvCity: storageProvCity
      })
    } else {
      app.getDaoPostN('api/FJDispose/FJ_Get_ProvCity', '{}', function (r) {
        if (r.code == 200) {
          wx.setStorageSync('storageProvCity', r.data);
          wx.setStorageSync('storageProvCity_tamp', timestamp + 604800000);
          r.data.splice(0, 1);
          that.setData({
            ProvCity: r.data
          })
        }
      })
    }
  },
  fadeIn: function () {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export()//动画实例的export方法导出动画数据传递给组件的animation属性
    })
  },
  fadeDown: function () {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
  zhuzhimaopao: function () {
    // body...
  },
  getRanNum: function () {
    var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
    var pwd = '';
    for (var i = 0; i < 4; i++) {
      if (Math.random() < 48) {
        pwd += chars.charAt(Math.random() * 48 - 1);
      }
    }
    return pwd;
  },
  changePyzm: function () {
    var that = this;
    var num = that.getRanNum();
    // console.log(num)
    this.setData({
      num: num
    })
    new Mcaptcha({
      el: 'canvas',
      width: 80,//对图形的宽高进行控制
      height: 30,
      code: num
    });
  },
  logingetyzm: function () {
    this.getCode();
  },
  logingetyzmN: function () {
    // body...
  },
  inputPyzm: function (e) {
    this.setData({
      inputPyzm: e.detail.value
    })
  },
  LinkPhone: function (e) {
    this.setData({
      LinkPhone: e.detail.value
    })
  },

  getCode: function () {
    var that = this;
    var tel = this.data.LinkPhone;
    var myreg = /^(14[0-9]|16[0-9]|19[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (!myreg.test(tel)) {
      app.tishi('请输入正确的手机号');
      return false;
    } else if (this.data.inputPyzm.toUpperCase() != this.data.num.toUpperCase()) {
      app.tishi('请输入正确的图形验证码');
      return false;
    } else {
      var api_input = {
        tel: this.data.LinkPhone
      }
      app.getDaoPost('api/FJDispose/Feijiu_GetTelYzm', api_input, function (r) {
        if (r.code == 200) {
          app.tishi(r.data.Mess);
          that.setData({
            iscode: app.aesDec(r.data.Yzm),
            disabled: false,
            LinkPhone1: tel,
          })

          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              that.setData({
                codename: '重新发送',
                disabled: true,
              })
            } else {
              that.setData({
                codename: num + "s后重新获取"
              })
            }
          }, 1000)

        } else {
          app.tishi(r.message)
        }
      })
    }
  },
})

